package cn.iocoder.yudao.module.erp.service.warehouserule;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.warehouserule.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.warehouserule.WarehouseRuleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.warehouserule.WarehouseRuleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * ERP 库房规则 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseRuleServiceImpl implements WarehouseRuleService {

    @Resource
    private WarehouseRuleMapper warehouseRuleMapper;

    @Override
    public Long createWarehouseRule(WarehouseRuleSaveReqVO createReqVO) {
        // 插入
        WarehouseRuleDO warehouseRule = BeanUtils.toBean(createReqVO, WarehouseRuleDO.class);
        warehouseRuleMapper.insert(warehouseRule);
        // 返回
        return warehouseRule.getId();
    }

    @Override
    public void updateWarehouseRule(WarehouseRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseRuleExists(updateReqVO.getId());
        // 更新
        WarehouseRuleDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseRuleDO.class);
        warehouseRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseRule(Long id) {
        // 校验存在
        validateWarehouseRuleExists(id);
        // 删除
        warehouseRuleMapper.deleteById(id);
    }

    private void validateWarehouseRuleExists(Long id) {
        if (warehouseRuleMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_RULE_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseRuleDO getWarehouseRule(Long id) {
        return warehouseRuleMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseRuleDO> getWarehouseRulePage(WarehouseRulePageReqVO pageReqVO) {
        return warehouseRuleMapper.selectPage(pageReqVO);
    }

}